package com.addcn.addcnwebview;

import android.content.Context;
import android.util.Log;


import java.io.File;


import ren.yale.android.cachewebviewlib.CacheType;
import ren.yale.android.cachewebviewlib.ResourceInterceptor;
import ren.yale.android.cachewebviewlib.WebViewCacheInterceptor;
import ren.yale.android.cachewebviewlib.WebViewCacheInterceptorInst;
import ren.yale.android.cachewebviewlib.config.CacheExtensionConfig;

/**
 * author:luwenbo
 * created on date : 2019-12-13
 * content:
 * describe:
 */
public class CustomWeb {
    private File mCacheFile;
    private long mCacheSize;
    private long mConnectTimeout;
    private long mReadTimeout;
    private CacheExtensionConfig mCacheExtensionConfig;
    private boolean mDebug;
    private CacheType mCacheType;
    //private String mAssetsDir = null;
    private boolean mTrustAllHostname = false;
    private ResourceInterceptor mResourceInterceptor;
    private boolean mIsSuffixMod = false;
    //==============
    private String mOrigin = "";
    private String mReferer = "";
    private String mUserAgent = "";
    public static final String KEY_CACHE = "WebResourceInterceptor-Key-Cache";
    private CustomWeb configBuild;

    public CustomWeb(Builder builder) {
        this.mCacheFile = builder.mCacheFile;
        this.mCacheSize = builder.mCacheSize;
        this.mCacheType = builder.mCacheType;
        this.mConnectTimeout = builder.mConnectTimeout;
        this.mReadTimeout = builder.mReadTimeout;
        this.mDebug = builder.mDebug;
        //this.mAssetsDir = builder.mAssetsDir;
        this.mTrustAllHostname = builder.mTrustAllHostname;
        this.mResourceInterceptor = builder.mResourceInterceptor;
        this.mIsSuffixMod = builder.mIsSuffixMod;
    }

    public void init(Context context){
        WebViewCacheInterceptor.Builder builder = new WebViewCacheInterceptor.Builder(context);
        builder.setCacheSize(mCacheSize);
        builder.setCachePath(mCacheFile);
        builder.setCacheType(mCacheType);
        builder.setConnectTimeoutSecond(mConnectTimeout);
        builder.setReadTimeoutSecond(mReadTimeout);
        builder.setDebug(mDebug);
        //builder.setAssetsDir(mAssetsDir);
        builder.setTrustAllHostname();
       // builder.setResourceInterceptor(mResourceInterceptor);
        builder.isAssetsSuffixMod(mIsSuffixMod);
        WebViewCacheInterceptorInst.getInstance().
                init(builder);
        //WebViewUtil.getInstance().loadCacheUrl("");
    }

    public static class Builder {

        private File mCacheFile;
        private long mCacheSize = 500 * 1024 * 1024;
        private long mConnectTimeout = 20;
        private long mReadTimeout = 20;
        private boolean mDebug = BuildConfig.DEBUG;
        private Context mContext;
        private CacheType mCacheType = CacheType.FORCE;


        private boolean mTrustAllHostname = false;
        private ResourceInterceptor mResourceInterceptor;

        private String mAssetsDir = null;
        private boolean mIsSuffixMod = false;
        public Builder(Context context) {
            mCacheFile = new File(context.getCacheDir().toString(), "CacheWebViewCache");
            //mCacheExtensionConfig = new CacheExtensionConfig();
        }

        public void setResourceInterceptor(ResourceInterceptor resourceInterceptor) {
            mResourceInterceptor = resourceInterceptor;
        }

        public Builder setTrustAllHostname() {
            mTrustAllHostname = true;
            return this;
        }


        public Builder setCachePath(File file) {
            if (file != null) {
                mCacheFile = file;
            }
            return this;
        }

        public Builder setCacheSize(long cacheSize) {
            if (cacheSize > 1024) {
                mCacheSize = cacheSize;
            }
            return this;
        }

        public Builder setReadTimeoutSecond(long time) {
            if (time >= 0) {
                mReadTimeout = time;
            }
            return this;
        }

        public Builder setConnectTimeoutSecond(long time) {
            if (time >= 0) {
                mConnectTimeout = time;
            }

            return this;
        }

//        public Builder setCacheExtensionConfig(CacheExtensionConfig config) {
//            if (config != null) {
//                mCacheExtensionConfig = config;
//            }
//            return this;
//        }

        public Builder setDebug(boolean debug) {
            mDebug = debug;
            return this;
        }

        public Builder setCacheType(CacheType cacheType) {
            mCacheType = cacheType;
            return this;
        }

        public Builder isAssetsSuffixMod(boolean suffixMod) {
            this.mIsSuffixMod = suffixMod;
            return this;
        }

        public Builder setAssetsDir(String dir) {
            if (dir != null) {
                mAssetsDir = dir;
            }
            return this;
        }


        public CustomWeb build() {
            return new CustomWeb(this);
        }

    }
}
